//
//  KKAdviceVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAdviceVC.h"
#import "KKAdviceCell.h"
#import "KKComHttp.h"
#import "KKAdviceSortDialog.h"
#import "KKAdviceItemM.h"
#import "KKPersonalHomeVC.h"
#import "KKApplyExpertTimeVC.h"
#import "KKUserInfoController.h"
#import "KKApplyExpertProgressVC.h"
#import "KKWebViewVC.h"
#import "KKAdviceCommentVC.h"
#import "KKApplyExpertProtocolVC.h"

@interface KKAdviceVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (copy,nonatomic) NSString *type;
@property (copy,nonatomic) NSString *order;
@property (strong,nonatomic) NSArray *adviceDatas;
@property (strong,nonatomic) NSArray *items;
@property (weak, nonatomic) IBOutlet UIView *shaiXuanView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollItemView;
@property (strong, nonatomic) UIButton *tempBtn;
@property(assign,nonatomic)float scrollWidth;

@end

@implementation KKAdviceVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.type=@"";
    self.order=@"";
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAdviceList)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKAdviceCell" bundle:nil] forCellReuseIdentifier:@"KKAdviceCell"];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sort)];
    [self.shaiXuanView addGestureRecognizer:tap];
    [self getAdviceItem];
}

-(void)initItemData{
    if(self.items!=nil&&self.items.count>0){

        self.scrollWidth=0;
        for(int i=0;i<self.items.count;i++){
            KKAdviceItemM *title=self.items[i];
            
            CGSize textSize=[KKAppUtils sizeWithText:title.param1 font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(1000, 40)];
            UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(self.scrollWidth,0, textSize.width+20, 40)];
            self.scrollWidth+=textSize.width+20;
            btn.tag=i;
            if(i==0){
                self.tempBtn=btn;
            }
            btn.selected=i==0?YES:NO;
            btn.titleLabel.font=[UIFont systemFontOfSize:14];
            [btn setTitle:title.param1 forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#1364CD"] forState:UIControlStateSelected];
            [self.scrollItemView addSubview:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.scrollItemView.contentSize = CGSizeMake(self.scrollWidth, 30);
    }
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKAdviceCell *advice=(KKAdviceCell *)cell;
    [advice initData:self.adviceDatas[indexPath.row]];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.adviceDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKAdviceCell"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 121;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKAdviceListM *advice=self.adviceDatas[indexPath.row];
    KKPersonalHomeVC *home=[[KKPersonalHomeVC alloc] init];
    home.expertId=advice.uid;
    [self.navigationController pushViewController:home animated:YES];
}

#pragma mark 按钮--官网消息与广告
- (IBAction)msgAndAdClick:(id)sender {
//    KKWebViewVC *webView=[[KKWebViewVC alloc] init];
//    webView.navTitle=@"官网消息与广告";
//    webView.url=@"http://www.baidu.com";
//    [self.navigationController pushViewController:webView animated:YES];
}

#pragma mark 按钮--筛选
-(void)sort{
    KKAdviceSortDialog *advice=[[[NSBundle mainBundle]loadNibNamed:@"KKAdviceSortDialog" owner:self options:nil] lastObject];
    [advice initData:self.items];
    advice.itemClickBlock=^(NSString *types,NSString *order){
        KKLog(@"types--->%@",types);
        self.type=types;
        self.order=order;
        [self.tableView.mj_header beginRefreshing];
    };
    [advice show];
}
#pragma mark 按钮--申请专家
- (IBAction)applyExpertClick:(id)sender {
    [self applyExpertProgress];
}


#pragma mark 头部item点击
-(void)btnClick:(UIButton *)btn{
    self.tempBtn.selected=NO;
    btn.selected=YES;
    self.tempBtn=btn;
    KKAdviceItemM *item=self.items[btn.tag];
    self.type=item.param1;
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark 接口--item
-(void)getAdviceItem{
    [KKComHttp getAdviceItem:^(id data) {
        self.items=data;
        [self initItemData];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--专家咨询列表
-(void)getAdviceList{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"type":self.type,@"order":self.order};
    [KKComHttp getAdviceList:params success:^(id data) {
        self.adviceDatas=data;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma mark 接口--申请专家进度
-(void)applyExpertProgress{
    [self showDialog];
    [KKComHttp applyExpertProgress:^(id data) {
        NSString *code=[data objectForKey:@"code"];
        if([code isEqualToString:@"ok"]){
            KKUserInfoController *user=[[KKUserInfoController alloc] init];
            user.flag=1;
            [self.navigationController pushViewController:user animated:YES];
        }else if ([code isEqualToString:@"list"]){
            KKApplyExpertProtocolVC *protocol=[[KKApplyExpertProtocolVC alloc] init];
            [self.navigationController pushViewController:protocol animated:YES];
        }else{
            NSString *msg=[data objectForKey:@"msg"];
            [self toast:msg];
        }
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
